%%%====================================================================
%%% File    : pipe_vect.erl
%%% Author  :  <vjache>
%%% Description: This is a simpliest pipe implementation. It may be 
%%%				 considered as a simple reference implementation of 
%%%				 a pipe callback module, and could be used for debug 
%%%				 purposes.
%%%
%%% Created : 24.03.2009
%%%====================================================================

-module(pipe_vect).

%%
%% Include files
%%
-include("../include/pipe.hrl").
-include("../include/util.hrl").
-include("../include/vect.hrl").
%%
%% Exported Functions
%%
-export([
		 new/1,
		 open/1,
		 push/2,
		 flush/1,
		 close/1,
		 test/1]).
-define(IS_DICT(ARG),element(1,ARG) == dict).
%%====================================================================
%% API Functions
%%====================================================================

new(VectByTagList) when is_list(VectByTagList)->
	pipe:pipe_from_module(?MODULE, _Param=VectByTagList, _NextPipe=terminal);
new(Vect) when is_record(Vect, vect)->
	pipe:pipe_from_module(?MODULE, _Param=Vect, _NextPipe=terminal);
new(BadVect) ->
	exit({bad_vect,BadVect}).

open({_Param=VectByTagList,terminal=_NextPipe}) when is_list(VectByTagList) -> %%PipeState
	dict:from_list([{Tag,vect:open_pipe(vect:open(Vect))}||{Tag,Vect}<-VectByTagList]);
open({_Param=Vect,terminal=_NextPipe}) -> %%PipeState
	VectSess=vect:open(Vect),
    VectSess.
push(VectSess,?EOF) ->
	VectSess;
push(PipeDict,Item) when ?IS_DICT(PipeDict) ->
	dict:update(
	  element(1,Item), 
	  fun(PipeState) ->
			  pipe:push(PipeState,Item)
	  end, PipeDict);
push(VectSess,Item) ->
    vect:insert(VectSess,Item),
	VectSess.
flush(VectSess) ->
	VectSess.
close(PipeDict) when ?IS_DICT(PipeDict) ->
	dict:fold(fun(_Tag,PipeState,_)->
					  ok=pipe:close(PipeState)
			  end, ok, PipeDict);
close(VectSess) ->
	ok=vect:close(VectSess).

%%====================================================================
%% Test Functions
%%====================================================================

test(L) ->
	Pipe=new(vect_mnesia:new_mem_bag(test, [a,b,c])),
	Pump=pump:new(iter:from_list(L), Pipe),
	pump:init(Pump).


%%====================================================================
%% Local Functions
%%====================================================================

